package cn.sumpu.app.guoguojie.biz.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cn.sumpu.app.guoguojie.biz.VisitorBiz;
import cn.sumpu.app.guoguojie.dao.VisitorDao;
import cn.sumpu.app.guoguojie.domain.Visitor;
import cn.sumpu.app.guoguojie.domain.Ext.PageModel;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;
import cn.sumpu.app.guoguojie.util.Common;

public class VisitorBizImpl implements VisitorBiz {

	private VisitorDao visitorDao;

	public void setVisitorDao(VisitorDao visitorDao) {
		this.visitorDao = visitorDao;
	}

	@Override
	public Visitor insertVisitor(Visitor visitor) throws AppBusinessException {
		visitorDao.insertVisitor(visitor);
		return visitor;
	}

	@Override
	public PageModel visitorMember(String doorplate, Integer page)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doorplate", doorplate);

		Integer startId = (page - 1) * Common.PAGE_SIZE_VISITOR;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_VISITOR);
		List<Visitor> vList = visitorDao.visitorMember(map);
		int count = visitorDao.visitorMemberCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(vList);
		pm.setPageSize(Common.PAGE_SIZE_VISITOR);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_VISITOR - 1)
				/ Common.PAGE_SIZE_VISITOR);
		return pm;
	}

	@Override
	public List<Visitor> visitorMemberSize(String doorplate, Integer size)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doorplate", doorplate);

		map.put("startId", 0);
		map.put("pageSize", size);
		return visitorDao.visitorMember(map);
	}

	@Override
	public PageModel visitorFamily(String visitMember, Integer page)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("visitMember", visitMember);

		Integer startId = (page - 1) * Common.PAGE_SIZE_VISITOR;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_VISITOR);
		List<Visitor> vList = visitorDao.visitorFamily(map);
		int count = visitorDao.visitorFamilyCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(vList);
		pm.setPageSize(Common.PAGE_SIZE_VISITOR);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_VISITOR - 1)
				/ Common.PAGE_SIZE_VISITOR);
		return pm;
	}

	@Override
	public PageModel taVisitorFamily(String family, Integer page)
			throws AppBusinessException {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("family", family);
		Integer startId = (page - 1) * Common.PAGE_SIZE_VISITOR;
		map.put("startId", startId);
		map.put("pageSize", Common.PAGE_SIZE_VISITOR);
		List<Visitor> vList = visitorDao.taVisitorFamily(map);
		int count = visitorDao.taVisitorFamilyCount(map);
		PageModel pm = new PageModel();
		pm.setCurrentPage(page);
		pm.setList(vList);
		pm.setPageSize(Common.PAGE_SIZE_VISITOR);
		pm.setTotalRecord(count);
		pm.setTotalPage((count + Common.PAGE_SIZE_VISITOR - 1)
				/ Common.PAGE_SIZE_VISITOR);
		return pm;
	}

	@Override
	public Visitor updateVisitor(Visitor visitor) throws AppBusinessException {
		visitorDao.updateVisitor(visitor);
		return visitor;
	}

	@Override
	public Visitor visitOtherFamily(String doorplate, String visitMember)
			throws AppBusinessException {
		if (Common.empty(doorplate)) {
			return null;
		}
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("doorplate", doorplate);
		map.put("visitMember", visitMember);
		Visitor v = visitorDao.getVisitorById(map);
		Visitor visitor = new Visitor();
		visitor.setDoorplate(doorplate);
		visitor.setVisitMember(visitMember);
		if (v != null) {
			visitorDao.updateVisitor(visitor);
		} else {
			visitorDao.insertVisitor(visitor);
		}
		return visitor;
	}

}
